package com.test.controller;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.R;
import com.test.adapter.SpinnerAdapter;
import com.test.entity.Icon;
import com.test.util.ImgUtil;
import com.test.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据模仿控制层（与之前一样）
 *
 * @Author Musique
 * 2020/8/5
 */
public class SpinnerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private Spinner spin_one, spin_two;
    private Context mContext;

    // 判断是否为刚进去触发OnItemSelected的标志
    private boolean one_select = false, two_select = false;
    private List<Icon> mIconList;
    private SpinnerAdapter mSpinnerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_demo);//把该类放在容器中
        mContext = this;
        behindViewData();
    }

    /**
     * 模拟数据源
     */
    public void behindViewData() {
        spin_one = (Spinner) findViewById(R.id.spin_one);
        spin_two = (Spinner) findViewById(R.id.spin_two);

        mIconList = new ArrayList<>();
        // 开始播放（线程）
        new Thread() {
            @Override
            public synchronized void run() {
                try {
                    for (int i = 1; i < 15; i++) {
                        Icon mIcon = new Icon();
                        String img = "iv_lol_icon" + i;
                        // mIcon.setAIcon(R.mipmap.iv_lol_icon1);
                        //ImgUtil.getResourceByReflect(img)这是获取图片id的工具类
                        mIcon.setAIcon(ImgUtil.getResourceByReflect(img));
                        // 工具类 英雄选择
                        mIcon.setTitle(StringUtil.heroSelection(i));
                        mIconList.add(mIcon);
                    }
                } catch (Exception e) {
                    System.err.println("意外停止");
                }
            }
        }.start();
        mSpinnerAdapter = new SpinnerAdapter(mIconList, mContext);
        spin_two.setAdapter(mSpinnerAdapter);
        // 设置监听事件
        spin_one.setOnItemSelectedListener(this);
        spin_two.setOnItemSelectedListener(this);
    }

    /**
     * 点击事件
     */
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        switch (adapterView.getId()) {
            case R.id.spin_one:
                if (one_select) {
                    Toast.makeText(mContext, "你选择的段位：" + adapterView.getItemAtPosition(i).toString(),
                            Toast.LENGTH_LONG).show();
                } else {
                    one_select = true;
                }
                break;
            case R.id.spin_two:
                // 此点击事件没进来
                System.out.println("**********");
                if (two_select) {
                    Toast.makeText(mContext, "你选择的英雄：" + mSpinnerAdapter.getItem(i),
                            Toast.LENGTH_LONG).show();
                } else {
                    two_select = true;
                }
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }
}
